/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import me.zhengjie.business.domain.Snry;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.SnryRepository;
import me.zhengjie.business.service.SnryService;
import me.zhengjie.business.service.dto.SnryDto;
import me.zhengjie.business.service.dto.SnryQueryCriteria;
import me.zhengjie.business.service.mapstruct.SnryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2022-04-13
**/
@Service
@RequiredArgsConstructor
public class SnryServiceImpl implements SnryService {

    private final SnryRepository snryRepository;
    private final SnryMapper snryMapper;

    @Override
    public Map<String,Object> queryAll(SnryQueryCriteria criteria, Pageable pageable){
        Page<Snry> page = snryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(snryMapper::toDto));
    }

    @Override
    public List<SnryDto> queryAll(SnryQueryCriteria criteria){
        return snryMapper.toDto(snryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SnryDto findById(String bisSnryId) {
        Snry snry = snryRepository.findById(bisSnryId).orElseGet(Snry::new);
        ValidationUtil.isNull(snry.getBisSnryId(),"Snry","bisSnryId",bisSnryId);
        return snryMapper.toDto(snry);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SnryDto create(Snry resources) {
        resources.setBisSnryId(IdUtil.simpleUUID());
        resources.setBisSnryHljgId(SecurityUtils.getCurrentUserId());
        return snryMapper.toDto(snryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Snry resources) {
        Snry snry = snryRepository.findById(resources.getBisSnryId()).orElseGet(Snry::new);
        ValidationUtil.isNull( snry.getBisSnryId(),"Snry","id",resources.getBisSnryId());
        snry.copy(resources);
        snryRepository.save(snry);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String bisSnryId : ids) {
            snryRepository.deleteById(bisSnryId);
        }
    }

    @Override
    public void download(List<SnryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SnryDto snry : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}